<template>
  <el-dialog :visible.sync="visible" custom-class="userDialog-wrapper" :before-close="handleClose">
    <el-form :model="factoryForm" ref="factory_form">
      <div class="userform-content">
        <el-row style="margin-bottom: 1vh">
          <el-col :span="24">
            <el-form-item :label-width="formLabelWidth" prop="name">
                <span slot="label" class="form-title">
                  场站名
                </span>
              <el-input v-model="factory_form.name"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="marg\n-bottom: 5vh">
          <el-col :span="24">
            <el-form-item :label-width="formLabelWidth" prop="typename">
                <span slot="label" class="form-title">
                  场站模块
                </span>
              <el-select v-model="factory_form.post" multiple collapse-tags style="width: 420px">
                <el-option
                  v-for="item in postOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item :label-width="formLabelWidth" prop="typename">
                <span slot="label" class="form-title, small_label">
                  1#动力电表倍数
                </span>
              <el-input v-model="factory_form.powerfirst"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label-width="formLabelWidth" prop="typename">
                <span slot="label" class="form-title, small_label">
                  2#动力电表倍数
                </span>
              <el-input v-model="factory_form.powersecond"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="margin-bottom: 5vh">
          <el-col :span="12">
            <el-form-item :label-width="formLabelWidth" prop="typename">
                <span slot="label" class="form-title, small_label">
                  照明电表倍数
                </span>
              <el-input v-model="factory_form.light"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </div>
    </el-form>
    <div slot="title" style="font-size: 20px; color: #060920" v-if="dialogKind === 0">
      修改信息
    </div>
    <div slot="title" style="font-size: 20px; color: #060920" v-else>
      添加信息
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button class="el-green-button" @click="submitForm('factory_form')" type="primary">保存</el-button>
      <el-button class="el-red-button" @click="handleClose" type="info">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import request from '../../network/request'
export default {
  name: 'currencyDialog',
  watch: {
    visibleFlag: function (newValue) {
      this.visible = newValue
    }
  },
  mounted () {
    this.visible = this.visibleFlag
  },
  data () {
    return {
      formLabelWidth: '120px',
      visible: false,
      factoryForm: {},
      postOptions: [{
        value: '生活垃圾机械化分类场站设备运行时间表',
        label: '生活垃圾机械化分类场站设备运行时间表'
      }, {
        value: '生活垃圾机械化分类场站堵塞清掏纪录表',
        label: '生活垃圾机械化分类场站堵塞清掏纪录表'
      }, {
        value: '生活垃圾机械化分类场站异常垃圾登记表',
        label: '生活垃圾机械化分类场站异常垃圾登记表'
      }, {
        value: '生活垃圾机械化分类场站除臭剂喷洒时间表',
        label: '生活垃圾机械化分类场站除臭剂喷洒时间表'
      }, {
        value: '生活垃圾机械化分类场站瓶分机可腐物分拣数据记录表',
        label: '生活垃圾机械化分类场站瓶分机可腐物分拣数据记录表'
      }, {
        value: '生活垃圾机械化分类场站易净场站出售废品登记表',
        label: '生活垃圾机械化分类场站易净场站出售废品登记表'
      }, {
        value: '生活垃圾机械化分类场站可燃物、可腐物储料箱清运记录表',
        label: '生活垃圾机械化分类场站可燃物、可腐物储料箱清运记录表'
      }, {
        value: '生活垃圾机械化分类场站可燃物、可腐物箱内剩余记录表',
        label: '生活垃圾机械化分类场站可燃物、可腐物箱内剩余记录表'
      }, {
        value: '生活垃圾机械化分类场站设备保养计划及记录表',
        label: '生活垃圾机械化分类场站设备保养计划及记录表'
      }, {
        value: '生活垃圾机械化分类场站故障维修原始记录表',
        label: '生活垃圾机械化分类场站故障维修原始记录表'
      }, {
        value: '生活垃圾机械化分类场站水记录表',
        label: '生活垃圾机械化分类场站水记录表'
      }, {
        value: '生活垃圾机械化分类场站电记录表',
        label: '生活垃圾机械化分类场站电记录表'
      }, {
        value: '生活垃圾机械化分类场站除臭塔换水记录',
        label: '生活垃圾机械化分类场站除臭塔换水记录'
      }, {
        value: '黑水虻培育岗位',
        label: '黑水虻培育岗位'
      }, {
        value: '黑水虻种虫养殖岗位',
        label: '黑水虻种虫养殖岗位'
      }, {
        value: '黑水虻垃圾处理岗位',
        label: '黑水虻垃圾处理岗位'
      }, {
        value: '黑水虻温湿度',
        label: '黑水虻温湿度'
      }, {
        value: '四季青有机资源再生中心称重岗位',
        label: '四季青有机资源再生中心称重岗位'
      }, {
        value: '四季青有机资源再生中心固液分离岗位',
        label: '四季青有机资源再生中心固液分离岗位'
      }, {
        value: '四季青有机资源再生中心絮凝岗位',
        label: '四季青有机资源再生中心絮凝岗位'
      }, {
        value: '四季青有机资源再生中心堆肥岗位',
        label: '四季青有机资源再生中心堆肥岗位'
      }, {
        value: '四季青有机资源再生中心水处理岗位',
        label: '四季青有机资源再生中心水处理岗位'
      }, {
        value: '四季青有机资源再生中心除臭岗位',
        label: '四季青有机资源再生中心除臭岗位'
      }, {
        value: '四季青有机资源再生中心系统修理岗位',
        label: '四季青有机资源再生中心系统修理岗位'
      }, {
        value: '工艺现场各工序用电记录表',
        label: '工艺现场各工序用电记录表'
      }, {
        value: '工艺现场工艺现场内控记录表',
        label: '工艺现场工艺现场内控记录表'
      }, {
        value: '工艺现场堆肥吨袋进、出料记录表',
        label: '工艺现场堆肥吨袋进、出料记录表'
      }, {
        value: '工艺现场大修现场记录表',
        label: '工艺现场大修现场记录表'
      }, {
        value: '生产现场固液分离生产现场记录表',
        label: '生产现场固液分离生产现场记录表'
      }, {
        value: '生产现场絮凝生产现场记录表',
        label: '生产现场絮凝生产现场记录表'
      }, {
        value: '生产现场堆肥生产现场记录表',
        label: '生产现场堆肥生产现场记录表'
      }, {
        value: '生产现场水处理生产现场记录表',
        label: '生产现场水处理生产现场记录表'
      }, {
        value: '生产现场除臭生产现场记录表',
        label: '生产现场除臭生产现场记录表'
      }],
      roleOptions: [{
        value: '管理人员',
        label: '管理人员'
      }, {
        value: '一线员工',
        label: '一线员工'
      }]
    }
  },
  props: {
    visibleFlag: { type: Boolean, required: true },
    dialogKind: { type: Number, required: true, default: 1 }, // 0:修改信息dialog；1：增加信息dialog
    factory_form: { type: Object, required: true },
    factoryId: { type: String }
  },
  methods: {
    handleClose () {
      this.$emit('close-dialog')
    },
    submitForm (formName) {
      // eslint-disable-next-line camelcase
      // const factory_form = this.factoryForm
      let baseurl
      switch (this.dialogKind) {
        case 0 : {
          // eslint-disable-next-line camelcase
          console.log('url update传入：' + this.factoryForm.id)
          console.log('传入场站包含模块：' + this.factoryForm.post)
          const form = new FormData()
          form.append('id', this.factory_form.id)
          form.append('name', this.factory_form.name)
          form.append('identification', this.factory_form.identification)
          form.append('post', this.factory_form.post)
          form.append('powerfirst', this.factory_form.powerfirst)
          form.append('powersecond', this.factory_form.powersecond)
          form.append('light', this.factory_form.light)
          baseurl = '/url/updateUrlPosition'
          request({
            url: baseurl,
            method: 'post',
            data: form
          }).then(res => {
            if (res.data.code === 200) {
              this.$message({
                type: 'success',
                message: '修改成功'
              })
              this.$emit('close-dialog')
            } else {
              this.$message({
                type: 'error',
                message: '修改失败'
              })
            }
          })

          baseurl = '/url/update'
          request({
            url: baseurl,
            method: 'post',
            data: form
          }).then(res => {
            if (res.data.code === 200) {
              this.$message({
                type: 'success',
                message: '修改成功'
              })
              this.$emit('close-dialog')
            } else {
              this.$message({
                type: 'error',
                message: '修改失败'
              })
            }
          })
          break
        }
        case 1 : {
          baseurl = '/url/addUrl'
          const form = new FormData()
          form.append('name', this.factory_form.name)
          form.append('identification', this.factory_form.identification)
          form.append('post', this.factory_form.post)
          form.append('powerfirst', this.factory_form.powerfirst)
          form.append('powersecond', this.factory_form.powersecond)
          form.append('light', this.factory_form.light)
          request({
            url: baseurl,
            method: 'post',
            data: form
          }).then(res => {
            if (res.data.code === 200) {
              this.$message({
                type: 'success',
                message: '添加成功'
              })
              this.$emit('close-dialog')
            } else {
              this.$message({
                type: 'error',
                message: '添加失败'
              })
            }
          })

          baseurl = '/url/addUrlPosition'
          const form1 = new FormData()
          form1.append('name', this.factory_form.name)
          form1.append('identification', this.factory_form.identification)
          form1.append('post', this.factory_form.post)
          form.append('powerfirst', this.factory_form.powerfirst)
          form.append('powersecond', this.factory_form.powersecond)
          form.append('light', this.factory_form.light)
          request({
            url: baseurl,
            method: 'post',
            data: form1
          }).then(res => {
            if (res.data.code === 200) {
              this.$message({
                type: 'success',
                message: '添加成功'
              })
              this.$emit('close-dialog')
            } else {
              this.$message({
                type: 'error',
                message: '添加失败'
              })
            }
          })
          break
        }
      }
    }
  }
}
</script>

<style scoped>
  .userform-content{
    padding: 20px;
    display: flex;
    margin-right: 50px;
    flex-direction: column;
    justify-content: center;
  }
  >>> .el-dialog__body {
    height: 35vh;
  }
  .small_label{
    font-size: 14px;
  }
</style>
